import 'package:flutter/material.dart';

class WyFutureBuilder extends StatefulWidget {
  @override
  _WyFutureBuilderState createState() => _WyFutureBuilderState();
}

class _WyFutureBuilderState extends State<WyFutureBuilder> {

 
  @override
  Widget build(BuildContext context) {
    int index = 1;
    return Scaffold(
      appBar: AppBar(
        title: Text('FutureBuilder'),
      ),
      body: index==0?_creactFutureBuilder():_createListView(),
    );
  }

  var _future = Future.delayed(Duration(seconds: 15), () {
//    return Future.error('');
    return '老王，一个有态度的程序员';
  });


  Future <Null> _future1()async{
    Future.delayed(Duration(seconds: 15));
    return Future.error('ff');

  }

  Widget _creactFutureBuilder() {
    return FutureBuilder(
        future: _future,
        builder: (BuildContext context, AsyncSnapshot<String> snapshot) {
          var widget;
          if (snapshot.connectionState == ConnectionState.done) {
            if (snapshot.hasError) {
              widget = Icon(
                Icons.error,
                color: Colors.red,
                size: 48,
              );
            } else {
              widget = Icon(
                Icons.check_circle,
                color: Colors.green,
                size: 36,
              );
            }
          } else {
            widget = Padding(
              padding: EdgeInsets.all(20),
              child: CircularProgressIndicator(),
            );
          }

          return Center(
            child: Container(
              height: 100,
              width: 100,
              decoration: BoxDecoration(
                  border: Border.all(color: Colors.grey),
                  borderRadius: BorderRadius.all(Radius.circular(10))),
              child: widget,
            ),
          );
        });
  }
 //构建FutureBuilder控件：
  Widget _createListView(){
    return FutureBuilder(
        future: _future,
        builder: (context, snapshot) {
          var widget;
          if (snapshot.connectionState == ConnectionState.done) {
            if (snapshot.hasError) {
              widget = _loadingErrorWidget();
            } else {
              widget = _dataWidget(snapshot.data);
            }
          } else {
            widget = _loadingWidget();
          }
          return widget;
        });
  }
  //构建loading控件：
  _loadingWidget(){
    return Center(
      child: Padding(
        padding: EdgeInsets.all(20),
        child: CircularProgressIndicator(),
      ),
    );
  }
  //数据加载成功，构建数据展示控件：
  _dataWidget(data){
      return ListView.separated(itemBuilder: (context,index){
        return Container(
          height: 60,
          alignment: Alignment.center,
          child: Text('$index',style: TextStyle(fontSize: 20),),
        );
      },
          separatorBuilder: (context,index){
            return Divider();
          },
          itemCount: 10);
  }
//构建网络加载失败控件：
  _loadingErrorWidget() {

    return Center(
      child: Text('数据加载失败，请重试。'),
    );
  }
}
